<template>
  <div>

      <div style="margin-bottom:15px">
        <el-input v-model="params.username" style="width:200px" placeholder="请输入账号查询"></el-input>
        
        <el-button type="warning" style="margin-left:10px" @click="findBySearch()">查询</el-button>
        <el-button type="warning" style="margin-left:10px" @click="reset()">清空</el-button></el-button>
        <!-- <el-button type="primary" style="margin-left:10px" @click="add()">添加</el-button>
        <el-button type="danger" style="margin-left:10px" @click="deleteBatch">批量删除</el-button> -->
      </div>

      <div class="operation">
        <el-button type="primary" style="margin-left:10px" @click="add()">添加</el-button>
        <el-button type="danger" style="margin-left:10px" @click="deleteBatch">批量删除</el-button>
      </div>

      <div class="table">
        <el-table :data="tableData" style="width: 100%" stripe @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>
          
          <!-- <el-table-column  label="头像" >
          <template v-slot="scope">
            <div>
              <el-image style="width: 70px; height: 70px; border: none; cursor: pointer" 
            :src="scope.row.avatar" :preview-src-list="[scope.row.avatar]"></el-image>

            </div>
          
          </template>
        </el-table-column> -->

        <el-table-column prop="avatar" label="头像" align="center">
          <template slot-scope="{ row }">
            <el-image style="width: 70px; height: 70px; border: none; cursor: pointer" :src="row.avatar"></el-image>
          </template>
        </el-table-column>

          <el-table-column prop="username" label="用户名" ></el-table-column>
          <el-table-column prop="name" label="姓名" ></el-table-column>
          <el-table-column prop="phone" label="电话"></el-table-column>
          <el-table-column prop="email" label="邮箱"></el-table-column>
          <el-table-column prop="role" label="角色"></el-table-column>
          <el-table-column prop="member" label="会员"></el-table-column>
          <el-table-column prop="score" label="积分"></el-table-column>
          <el-table-column prop="account" label="余额"></el-table-column>
          <el-table-column  label="操作" align="center" width="180px">
            <template v-slot="scope">
              <el-button size="mini" type="primary" plain @click="edit(scope.row)">编辑</el-button>
              
              <el-button size="mini" type="danger" plain style="margin-left:5px" @click="del(scope.row.id)">删除</el-button>
              
            </template>
          </el-table-column>
        </el-table>
      </div>

            <div style="margin-top: 10px;">
          
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
              <div>

                    <el-dialog title="会员" :visible.sync="fromVisible" width="30%">
            <el-form :model="form">

              <!-- <el-form-item label="头像">
                <el-upload
                   class="avatar-uploader"
                   :action="$baseUrl + '/files/upload'"
                   :headers="{token :user.token}"
                   list-type="picture"
                   :on-success="handleAvatarSuccess"
                   >
                   <el-button type="primary">上传头像</el-button>
                </el-upload>
              </el-form-item> -->

              <el-form-item label="头像" label-width="15%">
                <el-upload action="http://localhost:8080/api/files/upload" :on-success="successUpload">
                  <el-button size="small" type="primary">点击上传</el-button>            
                </el-upload>
              </el-form-item>

              <el-form-item label="用户名" label-width="15%">
                <el-input v-model="form.username" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="姓名" label-width="15%">
                <el-input v-model="form.name" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="电话" label-width="15%">
                <el-input v-model="form.phone" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <el-form-item label="邮箱" label-width="15%">
                <el-input v-model="form.email" autocomplete="off" style="width:90%"></el-input>
              </el-form-item>

              <!-- <el-form-item label="头像">
                <el-upload
                   class="avatar-uploader"
                   :action="$baseUrl + '/files/upload'"
                   :headers="{token :user.token}"
                   list-type="picture"
                   :on-success="handleAvatarSuccess"
                   >
                   <el-button type="primary">上传头像</el-button>
                </el-upload>
              </el-form-item> -->


              <!-- <el-form-item label="role" label-width="15%">
                <el-select v-model="form.role" placeholder="Please select" style="width:90%">
                  <el-option label="TUTOR" value="ROLE_TUTOR"></el-option>
                  <el-option label="STUDENT" value="ROLE_STUDENT"></el-option>
                </el-select>
              </el-form-item> -->
              
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="fromVisible = false">取消</el-button>
              <el-button type="primary" @click="submit()">确定</el-button>
            </div>
          </el-dialog>

        </div>

  </div>
</template>

<script>
import request from '@/utils/request'
import { MessageBox, Message } from 'element-ui';
    export default {
      name:"User",
      data() {
        return {
          params:{
            username:'',
            // phone:'',
            pageNum:1,
            pageSize:5
          },
          tableData: [],
          total: 0,
          fromVisible: false,
          form:{},
          multipleSelection:''  //当前表格选中的多个元素
        }
      },
      //页面加载的时候，做一些事情，在created里面
      created(){
         this.findBySearch()
      },
      //定义一些页面上空间触发的事件调用的方法
      methods:{
        
        findBySearch(){
          request.get("/user/page",{
            params:this.params
          }).then(res =>{
            if(res.code === '0'){
              this.tableData = res.data.list
              this.total = res.data.total
            }else{

            }
          })
        },
        add(){
          this.form = {}
          this.fromVisible = true
        },
        edit(obj){
          this.fromVisible = true
          this.form = obj

        },
        reset(){
           this.params={
            pageNum:1,
            pageSize:5,
            username:'',
            // phone:''
           }
           this.findBySearch()
        },
        handleSizeChange(pageSize){
           this.params.pageSize = pageSize
           this.findBySearch()
        },
        handleCurrentChange(pageNum){
          this.params.pageNum = pageNum
           this.findBySearch()
        },
        submit(){
          request.post("/user",this.form).then(res =>{
            if(res.code === '0'){
                this.$message.success("操作成功")
                this.fromVisible = false
                this.findBySearch()
            }else{
                            this.$message.error(res.msg)
            }
          })
        },
        del(id){

            MessageBox.confirm('确定删除吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              request.delete("/user/"+id).then(res =>{
             if(res.code === '0'){
               this.$message.success("删除成功")
            
              this.findBySearch()
            }else{
              this.$message.error(res.msg)
            }
          })
              // 这里放删除的逻辑
              // Message.success('删除成功');
            }).catch(() => {
              // 用户取消删除
              Message.info('取消删除');
            });
        },
        successUpload(res){
           console.log(res);
           this.form.avatar = res.data;
        },
        handleSelectionChange(val){
           this.multipleSelection = val
        },
        deleteBatch(){
            
            MessageBox.confirm('确定删除吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                  //alert(this.multipleSelection.length)

                  let ids = ''
                  const arr = new Array
                  this.multipleSelection.forEach(element => {
                    arr.push(element.id)
                  });
                  ids = arr.join(',')

                  // request.delete("/user/batch/"+param).then(res =>{

                  request.delete("/user/batch",{
                    params: {ids:ids}
                  }).then(res =>{
                    if(res.code === '0'){
                      this.$message.success("删除成功")
                    
                      this.findBySearch()
                    }else{
                      this.$message.error(res.msg)
                    }
                  })
                    // if(res.code === '0'){
                    //   this.$message.success("删除成功")
                    
                    //   this.findBySearch()
                    // }else{
                    //   this.$message.error(res.msg)
                    // }
                  // })
              // 这里放删除的逻辑
              // Message.success('删除成功');
            }).catch(() => {
              // 用户取消删除
              Message.info('取消删除');
            });

            


            // let param = ''
            // const arr = new Array
            // this.multipleSelection.array.forEach(element => {
            //   arr.push(element.id)
            // });
            // param = arr.join(',')

            // request.delete("/user/batch"+param).then(res =>{
            //   if(res.code === '0'){
            //     this.$message.success("删除成功")
              
            //     this.findBySearch()
            //   }else{
            //     this.$message.error(res.msg)
            //   }
            // })
        

          }
    }
}
  </script>